package com.estronger.shopmanage.ui.aftermarket.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.estronger.pull_to_refresh_expandablelist.CommonFooterView;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshBase;
import com.estronger.pull_to_refresh_expandablelist.PullToRefreshListView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B30BuildList;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.aftermarket.adapter.AftermarketAdapter;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketBean;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketItem;
import com.estronger.shopmanage.view.CircularProgress;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

public class AftermarketBaseFragment extends Fragment implements PullToRefreshBase.OnRefreshListener<ListView> {

    protected String getType;

    @BindView(R.id.loding_view)
    CircularProgress circularProgress;
    @BindView(R.id.empty_view)
    TextView emptyView;
    @BindView(R.id.success_listview)
    PullToRefreshListView listView;
    AftermarketAdapter adapter;
    List<AftermarketItem> beanList = new ArrayList<>();
    Gson gson = new Gson();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_aftermarket_y_layout,container,false);
        ButterKnife.bind(this,view);

        listView.setOnRefreshListener(this);
        listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        listView.setState(CommonFooterView.State.HIDE);

        adapter = new AftermarketAdapter(beanList,getContext());
        listView.setAdapter(adapter);
        setGetType();
        onRequstData();
        return view;
    }

    protected void setGetType() {
        if (getType.equals("1")){
            adapter.setViewType(AftermarketAdapter.ViewType.ONE);
        }else if (getType.equals("2")){
            adapter.setViewType(AftermarketAdapter.ViewType.TWO);
        }else {
            adapter.setViewType(AftermarketAdapter.ViewType.ALL);
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        circularProgress.setVisibility(View.VISIBLE);
        onRequstData();
    }

    void endRefresh(){
        listView.setState(CommonFooterView.State.HIDE);
        listView.onRefreshComplete();
        circularProgress.setVisibility(View.GONE);
        emptyView.setVisibility(beanList.size() > 0?View.GONE:View.VISIBLE);
        adapter.notifyDataSetChanged();
    }


    protected NetManager netManager = new NetManager() {
        @Override
        public void onNetWorkErorr(int code, String result) {
            endRefresh();
        }

        @Override
        public void onNetWorkSuccess(int code, String result) {
            Log.e("------claimgettasklist",result);
            AftermarketBean list = gson.fromJson(result,AftermarketBean.class);
            beanList.clear();
            beanList.addAll(list.getData());
            endRefresh();
        }
    };
    public void onRequstData() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        params.addBodyParameter("gettype", getType);
        params.addBodyParameter("sf_action", "claimgettasklist");
        netManager.connectWork(params, NetManager.SEND_LIST);
    }
}
